using AgentFrameworkDemo.Models;
using AgentFrameworkDemo.Workflows;

namespace AgentFrameworkDemo.Services;

/// <summary>
/// Microsoft Agent Framework 核心接口
/// 基于官方文档架构设计
/// </summary>

/// <summary>
/// Agent管理接口
/// </summary>
public interface IAgentManager
{
    /// <summary>
    /// 创建Agent
    /// </summary>
    Task<Agent> CreateAgentAsync(AgentDefinition definition);

    /// <summary>
    /// 获取Agent
    /// </summary>
    Task<Agent?> GetAgentAsync(string agentId);

    /// <summary>
    /// 更新Agent定义
    /// </summary>
    Task<Agent> UpdateAgentAsync(string agentId, AgentDefinition definition);

    /// <summary>
    /// 删除Agent
    /// </summary>
    Task DeleteAgentAsync(string agentId);

    /// <summary>
    /// 执行Agent任务
    /// </summary>
    Task<AgentResponse> ExecuteAsync(AgentRequest request);

    /// <summary>
    /// 获取Agent状态
    /// </summary>
    Task<AgentLifecycleState> GetAgentStateAsync(string agentId);

    /// <summary>
    /// 启动Agent
    /// </summary>
    Task StartAgentAsync(string agentId);

    /// <summary>
    /// 停止Agent
    /// </summary>
    Task StopAgentAsync(string agentId);

    /// <summary>
    /// 暂停Agent
    /// </summary>
    Task PauseAgentAsync(string agentId);

    /// <summary>
    /// 恢复Agent
    /// </summary>
    Task ResumeAgentAsync(string agentId);

    /// <summary>
    /// 获取所有Agent
    /// </summary>
    Task<List<Agent>> ListAgentsAsync();

    /// <summary>
    /// 获取Agent执行历史
    /// </summary>
    Task<List<AgentExecution>> GetExecutionHistoryAsync(string agentId, int limit = 10);

    /// <summary>
    /// 获取Agent统计信息
    /// </summary>
    Task<AgentStatistics> GetStatisticsAsync(string agentId);
}

/// <summary>
/// Workflow管理接口
/// </summary>
public interface IWorkflowManager
{
    /// <summary>
    /// 创建Workflow定义
    /// </summary>
    Task<WorkflowDefinition> CreateWorkflowAsync(WorkflowDefinition workflow);

    /// <summary>
    /// 获取Workflow定义
    /// </summary>
    Task<WorkflowDefinition?> GetWorkflowAsync(string workflowId);

    /// <summary>
    /// 更新Workflow定义
    /// </summary>
    Task<WorkflowDefinition> UpdateWorkflowAsync(string workflowId, WorkflowDefinition workflow);

    /// <summary>
    /// 删除Workflow定义
    /// </summary>
    Task DeleteWorkflowAsync(string workflowId);

    /// <summary>
    /// 执行Workflow
    /// </summary>
    Task<WorkflowExecution> ExecuteWorkflowAsync(string workflowId, WorkflowInput input);

    /// <summary>
    /// 获取Workflow执行状态
    /// </summary>
    Task<WorkflowExecution?> GetExecutionAsync(string executionId);

    /// <summary>
    /// 取消Workflow执行
    /// </summary>
    Task CancelExecutionAsync(string executionId);

    /// <summary>
    /// 暂停Workflow执行
    /// </summary>
    Task PauseExecutionAsync(string executionId);

    /// <summary>
    /// 恢复Workflow执行
    /// </summary>
    Task ResumeExecutionAsync(string executionId);

    /// <summary>
    /// 获取所有Workflow定义
    /// </summary>
    Task<List<WorkflowDefinition>> ListWorkflowsAsync();

    /// <summary>
    /// 获取Workflow执行历史
    /// </summary>
    Task<List<WorkflowExecution>> GetExecutionHistoryAsync(string workflowId, int limit = 10);

    /// <summary>
    /// 验证Workflow定义
    /// </summary>
    Task<WorkflowValidationResult> ValidateWorkflowAsync(WorkflowDefinition workflow);
}

/// <summary>
/// 工具管理接口
/// </summary>
public interface IToolManager
{
    /// <summary>
    /// 注册工具
    /// </summary>
    Task RegisterToolAsync(ITool tool);

    /// <summary>
    /// 获取工具
    /// </summary>
    Task<ITool?> GetToolAsync(string toolId);

    /// <summary>
    /// 执行工具
    /// </summary>
    Task<ToolResult> ExecuteToolAsync(string toolId, ToolParameters parameters);

    /// <summary>
    /// 获取所有工具
    /// </summary>
    Task<List<ITool>> ListToolsAsync();

    /// <summary>
    /// 注销工具
    /// </summary>
    Task UnregisterToolAsync(string toolId);
}

/// <summary>
/// 记忆管理接口
/// </summary>
public interface IMemoryManager
{
    /// <summary>
    /// 保存记忆
    /// </summary>
    Task SaveMemoryAsync(string agentId, MemoryItem memory);

    /// <summary>
    /// 搜索记忆
    /// </summary>
    Task<List<MemoryItem>> SearchMemoriesAsync(string agentId, string query, int limit = 10);

    /// <summary>
    /// 获取记忆
    /// </summary>
    Task<List<MemoryItem>> GetMemoriesAsync(string agentId, int limit = 50);

    /// <summary>
    /// 删除记忆
    /// </summary>
    Task DeleteMemoryAsync(string agentId, string memoryId);

    /// <summary>
    /// 清理旧记忆
    /// </summary>
    Task CleanupOldMemoriesAsync(string agentId, TimeSpan olderThan);
}

/// <summary>
/// 工具接口
/// </summary>
public interface ITool
{
    string Id { get; }
    string Name { get; }
    string Description { get; }
    List<ToolParameter> Parameters { get; }
    Task<ToolResult> ExecuteAsync(ToolParameters parameters);
}

/// <summary>
/// 工具参数
/// </summary>
public class ToolParameters
{
    public Dictionary<string, object> Values { get; set; } = new();
}

/// <summary>
/// 工具参数定义
/// </summary>
public class ToolParameter
{
    public string Name { get; set; } = string.Empty;
    public string Type { get; set; } = string.Empty;
    public string Description { get; set; } = string.Empty;
    public bool Required { get; set; } = true;
    public object? DefaultValue { get; set; }
}

/// <summary>
/// 记忆项
/// </summary>
public class MemoryItem
{
    public string Id { get; set; } = Guid.NewGuid().ToString();
    public string AgentId { get; set; } = string.Empty;
    public string Content { get; set; } = string.Empty;
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
    public Dictionary<string, object> Metadata { get; set; } = new();
    public List<string> Tags { get; set; } = new();
    public double Importance { get; set; } = 1.0;
}